package com.administration.controller;

import com.administration.constants.Result;
import com.administration.dao.SysNoticeMapper;
import com.administration.pojo.SysNotice;
import com.administration.utils.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysNotice")
public class SysNoticeController {

    @Resource
    private SysNoticeMapper sysNoticeMapper;

    //通知信息展示主界面
    @RequestMapping("/notice.do")
    public String noticeIndex() {
        return "/static/notice/layui_notice_index.html";
    }

    //查询所有通知信息
    @ResponseBody
    @RequestMapping("/selectAllNotice.do")
    public ResultData selectAllNotice(SysNotice sysNotice) {
        Map<String, Object> map = new HashMap<>();
        map.put("noticeId", sysNotice.getNoticeId());
        map.put("noticeType", sysNotice.getNoticeType());
        map.put("noticeTitle", sysNotice.getNoticeTitle());
        map.put("createBy", sysNotice.getCreateBy());

        List<SysNotice> list = sysNoticeMapper.selectAllNotice(map);
        for (SysNotice sn : list) {
            //修改状态显示
            if ("1".equals(sn.getNoticeType())) {
                sn.setNoticeType("通知");
            } else {
                sn.setNoticeType("公告");
            }
            //修改状态显示
            if ("0".equals(sn.getStatus())) {
                sn.setStatus("正常");
            } else {
                sn.setStatus("关闭");
            }
        }

        return new ResultData<>(Result.SUCCESS.getCode(), "", list.size(), list);
    }

    //新增通知信息
    @ResponseBody
    @RequestMapping("/addNotice.do")
    public int addNotice(SysNotice sysNotice) {
        int addRes = sysNoticeMapper.addNotice(sysNotice);
        return addRes;
    }

    //修改通知信息
    @ResponseBody
    @RequestMapping("/updateNotice.do")
    public int updateNotice(SysNotice sysNotice) {
        int updateRes = sysNoticeMapper.updateNotice(sysNotice);
        return updateRes;
    }

    //删除通知信息
    @ResponseBody
    @RequestMapping("/deleteNotice.do")
    public int deleteNotice(SysNotice sysNotice) {
        int deleteRes = sysNoticeMapper.deleteNotice(sysNotice.getNoticeId());
        return deleteRes;
    }
}
